// --------------------------------------------------------------------------------------------------------------------
// <copyright file="UIIntrospectionReport.cs" company="Open Trader">
//   Copyright (c) David Denis (david.denis@systemathics.com)
// </copyright>
// <summary>
//   |  Open Trader - The Open Source Systematic Trading Platform
//   |
//   |  This program is free software: you can redistribute it and/or modify
//   |  it under the terms of the GNU General Public License as published by
//   |  the Free Software Foundation, either version 2 of the License, or
//   |  (at your option) any later version.
//   |
//   |  This program is distributed in the hope that it will be useful,
//   |  but WITHOUT ANY WARRANTY; without even the implied warranty of
//   |  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//   |  GNU General Public License for more details.
//   |
//   |  You should have received a copy of the GNU General Public License
//   |  along with this program.  If not, see http://www.gnu.org/licenses
//   |
//   |  Up to date informations about Open Trader can be found at :
//   |    http://opentrader.org
//   |    http://opentrader.codeplex.com
//   |
//   |  For professional services, please visit us at :
//   |    http://www.systemathics.com
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Org.OpenTrader.Framework.UI.Forms
{
    #region Using Directives

    using System.Collections.Generic;

    using Org.OpenTrader.Framework.Enums;
    using Org.OpenTrader.Framework.Events;
    using Org.OpenTrader.Framework.LiveObjects.Interfaces;

    #endregion

    /// <summary>
    /// The ui introspection report.
    /// </summary>
    public partial class UIIntrospectionReport : UIControl
    {
        #region Constants and Fields

        /// <summary>
        /// The allTypes.
        /// </summary>
        private IList<Item> allTypes = new List<Item>();

        /// <summary>
        /// The container.
        /// </summary>
        private ILiveObjectContainer container;

        #endregion

        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="UIIntrospectionReport"/> class.
        /// </summary>
        public UIIntrospectionReport()
        {
            this.InitializeComponent();
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="UIIntrospectionReport"/> class.
        /// </summary>
        /// <param name="container">
        /// The container.
        /// </param>
        /// <param name="rowsToDisplay">
        /// The rowsToDisplay.
        /// </param>
        public UIIntrospectionReport(ILiveObjectContainer container, int rowsToDisplay)
        {
            this.container = container;
            foreach (var v in container.AllTypes)
            {
                this.allTypes.Add(new Item(v, EItemType.Class));
            }
        }

        #endregion

        #region Public Methods

        /// <summary>
        /// The ui grid query cell info.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        public void UIGridQueryCellInfo(object sender, VirtualGridEventArgs.QueryCellInfo e)
        {
            if (e.RowIndex >= this.allTypes.Count)
            {
                return;
            }

            var item = this.allTypes[e.RowIndex];

            // According the column display required data
            switch (e.ColumnIndex)
            {
                case 0: // type
                    e.Value = item.Type;
                    break;

                case 1: // class
                    e.Value = item.Class;
                    break;

                case 2: // assembly
                    if (item.Type == EItemType.Builder)
                    {
                        e.Value = this.container.GetContainingAssemblyName(item.Class);
                    }
                    else if (item.Type == EItemType.Class)
                    {
                        e.Value = this.container.GetContainingAssemblyName(item.Class);
                    }

                    break;
            }
        }

        /// <summary>
        /// The ui grid query row count.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        public void UIGridQueryRowCount(object sender, VirtualGridEventArgs.QueryRowCount e)
        {
            e.RowCount = this.allTypes.Count; // Math.Max(allTypes.Length, rowsToDisplay);
        }

        #endregion

        #region Methods

        /// <summary>
        /// The ui virtual grid 1_ ui grid query cell info.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        private void OnVirtualGrid1UIGridQueryCellInfo(object sender, VirtualGridEventArgs.QueryCellInfo e)
        {
        }

        /// <summary>
        /// The ui virtual grid 1_ ui grid query row count.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        private void OnVirtualGrid1UIGridQueryRowCount(object sender, VirtualGridEventArgs.QueryRowCount e)
        {
        }

        #endregion

        /// <summary>
        /// The item.
        /// </summary>
        internal class Item
        {
            #region Constructors and Destructors

            /// <summary>
            /// Initializes a new instance of the <see cref="Item"/> class.
            /// </summary>
            /// <param name="classname">
            /// The classname.
            /// </param>
            /// <param name="type">
            /// The type.
            /// </param>
            public Item(string classname, EItemType type)
            {
                this.Class = classname;
                this.Type = type;
            }

            #endregion

            #region Properties

            /// <summary>
            /// The class.
            /// </summary>
            public string Class { get; set; }

            /// <summary>
            /// The type.
            /// </summary>
            public EItemType Type { get; set; }

            #endregion
        }
    }
}